package org.wabson.alfresco.translations.bean;

import java.util.ArrayList;
import java.util.List;

import org.jets3t.service.S3ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

public class JetS3tJarListLoader implements JarListLoader {

    private String awsAccessKey;
    
    private String awsSecretKey;
    
    private String bucketName;
    
    private String bucketPath;

	@Override
	public List<Jarfile> getJars()
	{
		ArrayList<Jarfile> jars = new ArrayList<Jarfile>();
		
        AWSCredentials awsCredentials = 
                new AWSCredentials(awsAccessKey, awsSecretKey);
        try
        {
            RestS3Service s3Service = new RestS3Service(awsCredentials);
            
            // Get the JAR file path
            S3Object object = s3Service.getObject(bucketName, bucketPath);

            if (object == null)
            {
                throw new IllegalArgumentException("The directory " + bucketPath + " was not found");
            }
            
            if (!object.isDirectoryPlaceholder())
            {
                throw new IllegalArgumentException("The item " + bucketPath + " is not a directory");
            }
            
            S3Object[] filteredObjects = s3Service.listObjects(bucketName, bucketPath, "/");
            for (int i = 0; i < filteredObjects.length; i++)
            {
                S3Object o = filteredObjects[i];
                if (!o.isDirectoryPlaceholder())
                {
                    Jarfile jar = new Jarfile(o.getKey().replaceFirst(bucketPath, ""), o.getName().replaceFirst(bucketPath, ""));
                    jar.setCreationDate(o.getLastModifiedDate());
                    jar.setSize(o.getContentLength());
                    jars.add(jar);
                }
            }
            // TODO sort by modification date
            //Collections.sort(jars, new BlobInfoDateComparator());
        }
        catch (S3ServiceException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
		return jars;
	}

	@Override
	public Jarfile getJar(String id)
	{
        AWSCredentials awsCredentials = 
                new AWSCredentials(awsAccessKey, awsSecretKey);
        try
        {
            RestS3Service s3Service = new RestS3Service(awsCredentials);
            S3Object object = s3Service.getObject(bucketName, bucketPath + id);
            Jarfile jar = new Jarfile(id.replaceFirst(bucketPath, ""), object.getName().replaceFirst(bucketPath, ""));
            jar.setCreationDate(object.getLastModifiedDate());
            jar.setSize(object.getContentLength());
            return jar;
        }
        catch (S3ServiceException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
	}

    public String getAwsAccessKey()
    {
        return awsAccessKey;
    }

    public void setAwsAccessKey(String awsAccessKey)
    {
        this.awsAccessKey = awsAccessKey;
    }

    public String getAwsSecretKey()
    {
        return awsSecretKey;
    }

    public void setAwsSecretKey(String awsSecretKey)
    {
        this.awsSecretKey = awsSecretKey;
    }

    public String getBucketName()
    {
        return bucketName;
    }

    public void setBucketName(String bucketName)
    {
        this.bucketName = bucketName;
    }

    public String getBucketPath()
    {
        return bucketPath;
    }

    public void setBucketPath(String bucketPath)
    {
        this.bucketPath = bucketPath;
    }

}
